<template>
  <el-row type="flex">
    <el-col :span="8">
      <el-card class="userCard">
        <div slot="header" class="clearfix cardHeader">
          <img src="../assets/img/head.jpg" class="headerImg">
          <div class="cardUser">
            <span class="userId">A90NY</span>
            <span class="identity">管理员</span>
          </div>
        </div>
        <div class="text">
          <div class="textBox">
            <p>上次登陆时间：<span>2022-9-10</span></p>
            <p>上次登陆地点：<span>四川 成都</span></p>
          </div>
        </div>
      </el-card>
      <el-card class="tabCard">
        <el-table :data="tableData" :default-sort = "{prop: 'totalSales',order: 'descending'}" height="427px" size="small" border style="width: 100%">
          <el-table-column prop="name" label="品牌" >
          </el-table-column>
          <el-table-column prop="totalSales" sortable label="总销量">
          </el-table-column>
          <el-table-column prop="monthlySales" sortable label="月销量">
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" class="elColSecond">
      <div class="orderBox">
        <div class="dataCards" v-for="(item,index) in otherData" :key="index">
        <i :class="item.icon" :style="item.bgaColor"></i>
        <div class="PBox">
          <p class="firstP">{{item.data}}</p>
          <p class="secondP">{{item.text}}</p>
        </div>
        </div>
      </div>
      <div class="LineChartBox" ref="LineChartBox">
      </div>
      <div class="bottomCharts">
        <div class="histogramBox" ref="histogramBox"></div>
        <div class="pieChartBox" ref="pieChartBox"></div>
      </div>
    </el-col>
  </el-row>
</template>
  
<script>
import { getHomeData } from '../../api/data';
import * as echarts from 'echarts';
export default {
  name: 'Home',
  data() {
    return {
      tableData: [],
      otherData:[]
    }
  },
  mounted(){
    //获取数据
    getHomeData().then(res=>{
      const {code,data} = res.data;
      if(code == 999){

        //表格
        this.tableData = data.tableData;

        //顶部六个卡片数据
        this.otherData = data.otherData;

        //折线图
        const lineChartBox = echarts.init(this.$refs.LineChartBox);
        let lineChartBoxSeries = [];
        const order = data.orderData
        const keyArr = Object.keys(order.data[0]);
        keyArr.forEach((value)=>{
          let dataObj = {
            name: value,
            type: 'line',
            data:order.data.map(item => item[value])
          }
          lineChartBoxSeries.push(dataObj);
        })
        let lineChartOption = {
          title:{
            text:'日销量',
            x:'center',
            y:230,
            textStyle:{
              fontSize:16,
            }
          },
          xAxis: {
            data: order.date
          },
          yAxis: {},
          series:lineChartBoxSeries,
          legend:{
              data:keyArr,
          },
        };
        lineChartBox.setOption(lineChartOption);
        
        //柱状图
        const histogramBox = echarts.init(this.$refs.histogramBox);
        let histogramBoxSeries = [];
        let histogramBoxXAxis = [];
        data.lastWeekOrderData.forEach((value)=>{
          histogramBoxXAxis.push(value.date);
          histogramBoxSeries.push(value.new);
        })
        let histogramOption = {
          title:{
            text:'上周新增订单',
            x:'center',
            y:210,
            textStyle:{
              fontSize:14,
            }
          },
          xAxis: {
            data: histogramBoxXAxis
          },
          yAxis: {},
          series:{
              type: 'bar',
              data: histogramBoxSeries,
              label:{
                show:false,
                position:'top',
              },
              emphasis:{
                label:{
                  show:true,
                }
              }
          },
          color:['rgb(185,166,225)'],
        };
        histogramBox.setOption(histogramOption);

        //饼图
        const pieChartBox = echarts.init(this.$refs.pieChartBox);
        let pieChartOption = {
          title:{
            text:'上周销量占比',
            x:10,
            textStyle:{
              fontSize:14,
            }
          },
          series:{
              type: 'pie',
              data: data.videoData,
          },
          color:[
            'rgb(205,175,209)',
            'rgb(244,155,161)',
            'rgb(248,189,159)',
            'rgb(153,213,175)',
            'rgb(245,153,204)',
            'rgb(191,218,119)',
            'rgb(101,197,239)',
            'rgb(110,235,250)',
            'rgb(255,235,98)',
            'rgb(213,214,232)',
            'rgb(102,219,233)',
            'rgb(253,245,159)',
          ],
        };
        pieChartBox.setOption(pieChartOption);
        pieChartBox.on('mouseover',function(params){
          const {name,value} = params.data;
          pieChartBox.setOption({
            title:{
              text:name,
              subtext:value+'%',
              left:'center',
              top:'105',
            },
            series: [{
              type: 'pie',
              radius: ['40%', '75%'],
            }]
          })
        })
        //图表响应式
        const elementResizeDetectorMaker = require("element-resize-detector");
        const erd = elementResizeDetectorMaker();
        erd.listenTo(this.$refs.LineChartBox, function(){   
          lineChartBox.resize();
          histogramBox.resize();
          pieChartBox.resize();
        });
      }
    })
  }
}
</script>

<style lang="less" scoped>
.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.box-card {
  width: 480px;
}

.cardHeader {
  line-height: 20px;
}

.userCard {
  .cardHeader {
    display: flex;
    justify-content: center;
    align-items: center;

    .headerImg {
      width: 100px;
      border-radius: 50px;
    }

    .cardUser {
      display: flex;
      flex-direction: column;
      justify-content: flex-end;
      margin: 30px 0 0 40px;

      .userId {
        font-size: 22px;
        font-weight: bold;
      }

      .identity {
        margin-top: 10px;
        font-size: 12px;
        color: #666262 !important;
      }
    }
  }

  .text {
    display: flex;
    justify-content: center;
    width: 100%;
    padding: 0px !important;
    color: #666262 !important;
    font-size: 12px;

    .textBox {
      display: flex;
      margin-left: -25px;
      flex-direction: column;
      width: 176px;
      text-align: left;

      p {
        height: 20px;
        width: 500px;
        line-height: 20px;
        margin: 0 0 0 10px;

        span {
          display: inline-block;
          margin-left: 40px;
        }
      }
    }
  }
}

.tabCard {
  margin-top: 20px;
  .el-table{
    line-height: 0px;
  }
}

.elColSecond{
  margin-left:20px ;
  .orderBox{
    display: flex;
    height: 150px;
    flex-wrap: wrap;
    justify-content: space-between;
    align-content: space-between;
    .dataCards{
      display: flex;
      height: 70px;
      width: 32%;
      line-height: 0px !important;
      overflow: hidden;
      box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
      border-radius: 4px;
      i{
          display: inline-block;
          height: 100%;
          width: 70px;
          text-align: center;
          line-height: 70px;
          font-size: 24px;
      }
      .PBox{
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-content: center;
        overflow: hidden;
        background-color: rgb(255,255,255);
        .firstP{
          line-height: 30px;
          font-size: 20px;
          font-weight: bold;
        }
        .secondP{
          line-height: 20px;
          font-size: 14px;
          color: #666262;
        }
      }
    }
  }
  .LineChartBox{
    display: flex;
    justify-content: center;
    align-items: center;
    height: 268px;
    margin-top: 20px;
    background-color: rgb(255,255,255);
    box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
    border-radius: 4px;
  }
  .bottomCharts{
    display: flex;
    height: 250px;
    margin-top: 20px;
    .histogramBox{
      flex:1;
      display: flex;
      justify-content: center;
      align-items: center;
      background-color: rgb(255,255,255);
      box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
      border-radius: 4px;
    }
    .pieChartBox{
      flex:1;
      display: flex;
      justify-content: center;
      align-items: center;
      margin-left: 20px;
      background-color: rgb(255,255,255);
      box-shadow: 0 2px 12px 0 rgb(0 0 0 / 10%);
      border-radius: 4px;
    }
  }
}
</style>